local skill = fk.CreateSkill{
  name = "tea__zhujing",
}
---@type TrigSkelSpec<EventPhaseChangingFunc>
local zhujing = {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player == target and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local suc, dat = room:askToUseActiveSkill(player, {
      skill_name = "tea__zhujing_active",
      prompt = "#tea__zhujing-invoke"
    })
    if suc then
      event:setCostData(self, dat)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local card = Fk:cloneCard(dat.interaction)
    card:addSubcards(dat.cards)
    card.skillName = skill.name
    local use = {
      from = player,
      tos = dat.targets,
      card = card,
    }
    room:useCard(use)
    if not player.dead then
      room:setPlayerMark(player, "@tea__zhujing_mark", {Fk:getCardById(dat.cards[1]).trueName, card.trueName})
      for _, p in ipairs(room.alive_players) do
        room:setPlayerMark(p, "tea__zhujing_trans", {Fk:getCardById(dat.cards[1]).trueName, card.trueName})
        if not p:hasSkill(Fk.skills["tea__zhujing_other&"]) then
          room:handleAddLoseSkills(p, "tea__zhujing_other&", nil, true)
        end
      end
    end
  end,
}
---@type TrigSkelSpec<TrigFunc>
local zhujing_re = {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skill.name, true, true) and player == target
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@tea__zhujing_mark", 0)
    for _, p in ipairs(room.alive_players) do
      room:setPlayerMark(p, "tea__zhujing_trans", 0)
      if p:hasSkill(Fk.skills["tea__zhujing_other&"], true) then
        room:handleAddLoseSkills(p, "-tea__zhujing_other&", nil, true)
      end
    end
  end,
}
---@type TrigSkelSpec<TrigFunc>
local zhujing_check = {
  can_refresh = function(self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return data == Fk.skills["tea__zhujing"]
    elseif event == fk.BuryVictim then
      return target:hasSkill(skill.name, true, true)
    elseif event == fk.AfterPropertyChange then
      return target == player
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local need = table.find(room.alive_players, function (p)
      return p:hasSkill(skill.name, true)
    end) and table.find(room.alive_players, function (p)
      return p:getMark("@tea__zhujing_mark") ~= 0
    end)
    if need and not player:hasSkill("tea__zhujing_other&", true, true) then
      room:handleAddLoseSkills(player, "tea__zhujing_other&", nil, true, false)
    elseif not need and player:hasSkill("tea__zhujing_other&", true, true) then
      room:handleAddLoseSkills(player, "-tea__zhujing_other&", nil, true, false)
    end
  end,
}
skill:addEffect(fk.EventPhaseStart, zhujing)
skill:addEffect(fk.TurnStart, zhujing_re)
skill:addEffect(fk.EventAcquireSkill, zhujing_check)
skill:addEffect(fk.EventLoseSkill, zhujing_check)
skill:addEffect(fk.BuryVictim, zhujing_check)
skill:addEffect(fk.AfterPropertyChange, zhujing_check)
return skill